package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.DashboardVo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GoodsProductMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatServiceImpl implements StatService{

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    OrderMapper orderMapper;


    @Override
    public DashboardVo dashboard() {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false);
        long goodsTotal = goodsMapper.countByExample(goodsExample);

        UserExample userExample = new UserExample();
        userExample.createCriteria().andDeletedEqualTo(false);
        long userTotal = userMapper.countByExample(userExample);

        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andDeletedEqualTo(false);
        long productTotal = goodsProductMapper.countByExample(goodsProductExample);

        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andDeletedEqualTo(false);
        long orderTotal = orderMapper.countByExample(orderExample);
        return new DashboardVo(goodsTotal, userTotal, productTotal, orderTotal);
    }

    private Map getStat(String[] columns,List stat) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("columns", columns);
        map.put("rows", stat);
        return map;
    }

    @Override
    public Map userStat() {
        List<StatUser> statUser = userMapper.statUsersGroupByAddtime();
        String[] columns = {"day", "users"};
        return getStat(columns,statUser);
    }

    @Override
    public Map orderStat() {
        List<StatOrder> statOrder = orderMapper.statOrdersGroupByAddtime();
        String[] columns = {"day","orders","customers","amount","pcr"};
        return getStat(columns,statOrder);
    }

    @Override
    public Map goodsStat() {
        List<StatGoods> statUser = goodsMapper.statGoodsGroupByAddtime();
        String[] columns = {"day","orders","products","amount"};
        return getStat(columns,statUser);
    }
}
